home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15229 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  94 lines

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Did I Miss Something?
  5. Date: Thu, 04 Apr 1996 12:54:47 +0200
  6. Organization: Carelcomp Products
  7. Message-ID: <3163AA77.5901@cmt.lpr.mail.carel.fi>
  8. References: <N.040396.105136.51@ix.netcom.com>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Jerry Hewett wrote:
  16. > [snip]
  17. > // OBJSTRNG.CPP, from Chapter 6 - More Encapsulation.
  18. > class box {
  19. >     int length;
  20. >     int width;
  21. >     char *line_of_text;
  22. > public:
  23. >     box(char *input_line);
  24. >     void set(int new_length, int new_width);
  25. >     int get_area(void);
  26. > };
  27. > /*
  28. >     Just guessing, but here goes...
  29. >     Below is the constructor for "box".  The object doesn't exist until the
  30. >     constructor creates it.  Since a pointer to a "known" fixed-length string
  31. >     is being passed to the constructor, the compiler determines the amount of
  32. >     memory required to store the string "small box ", allocates space for it
  33. >     in the object, and copies it into this space.
  34. >     Once the object passes out of scope this "magically" created chunk of
  35. >     memory allocation disappears along with the object... (???)
  36.  
  37. Not quite - compiler does not determine and allocate anything. main() calls the 
  38. constructor with the parameter "small box". This is a constant character string (read: 
  39. array ending with a '\0') that is stored somewhere in the memory. The assignment 
  40. line_of_text = input_line simply assigns the address of this string to line_of_text, 
  41. without allocating any memory. Similarly, there's no destructor, so the memory is not 
  42. freed, and of course should not be, because it was not allocated by the ctor in the 
  43. first place.
  44.  
  45. > */
  46. > box::box(char *input_line)
  47. > {
  48. >     length = 8;
  49. >     width = 8;
  50. >     line_of_text = input_line;
  51. > }
  52.  
  53. To make the ctor allocate some memory, one would use something like
  54.  
  55.     box::box(char *input_line)
  56.     {
  57.         // line_length just added for clarity
  58.         line_length = strlen(input_line) + 1;
  59.         line_of_text = new char[line_length];
  60.         strcpy(line_of_text, input_line);
  61.     }
  62.  
  63. and provide a destructor
  64.  
  65.     box::~box()
  66.     {
  67.         line_length = 0;
  68.         delete [] line_of_text;
  69.         line_of_text = 0;
  70.     }
  71.  
  72. The zeroings here are to provide that you won't use the members after destruction (or 
  73. if you try to, you'll probably have a core dump or a GPF). Of course, in this example, 
  74. line_length and destructor would have been added to the class declaration also.
  75.  
  76. > main()
  77. > {
  78. >     box small("small box ");
  79. > }
  80.  
  81. Later,
  82.  AriL
  83. -- 
  84. All my opinions are mine and mine alone.
  85.